﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using RiaToDalLinq.Configuration.Types;
using System.Xml;
using System.ComponentModel;
using RiaToDalLinq.Configuration.TypeConverters;

namespace RiaToDalLinq.Configuration
{
    public class RiaToDalLinqConfiguration : ConfigurationSection
    {
        [TypeConverter(typeof(RiaToDalLinqResolverTypeConverter))]
        [ConfigurationProperty("resolverType", IsRequired = true)]
        public RiaToDalLinqResolverType ResolverType
        {
            get
            {
                return (RiaToDalLinqResolverType)this["resolverType"];
            }
            set
            {
                this["resolverType"] = value;
            }
        }

        [ConfigurationProperty("customResolverPluginDirectory", DefaultValue = "", IsRequired = false)]
        public string CustomResolverPluginDirectory
        {
            get
            {
                return (string)this["customResolverPluginDirectory"];
            }
            set
            {
                this["customResolverPluginDirectory"] = value;
            }
        }

        [ConfigurationProperty("customResolverTypeName", DefaultValue = "", IsRequired = false)]
        public string CustomResolverTypeName
        {
            get
            {
                return (string)this["customResolverTypeName"];
            }
            set
            {
                this["customResolverTypeName"] = value;
            }
        }

        [ConfigurationProperty("dalProviderInstance", DefaultValue = "", IsRequired = false)]
        public string DalProviderInstance
        {
            get
            {
                return (string)this["dalProviderInstance"];
            }
            set
            {
                this["dalProviderInstance"] = value;
            }
        }

        [ConfigurationProperty("entityManagerNameFormatString", DefaultValue = "", IsRequired = false)]
        public string EntityManagerNameFormatString
        {
            get
            {
                return (string)this["entityManagerNameFormatString"];
            }
            set
            {
                this["entityManagerNameFormatString"] = value;
            }
        }

        protected override void PostDeserialize()
        {
            if (ResolverType == RiaToDalLinqResolverType.Configuration)
            {
                if (string.IsNullOrEmpty(DalProviderInstance) || string.IsNullOrEmpty(EntityManagerNameFormatString))
                {
                    throw new ApplicationException("DalProviderInstance and EntityManagerNameFormatString are required for configuration resolver type");
                }
            }
            else if (ResolverType == RiaToDalLinqResolverType.Custom)
            {
                if (string.IsNullOrEmpty(CustomResolverPluginDirectory) || string.IsNullOrEmpty(CustomResolverTypeName))
                {
                    throw new ApplicationException("CustomResolverPluginDirectory and CustomResolverTypeName are required for configuration resolver type");
                }
            }

            base.PostDeserialize();
        }
    }
}
